home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / diag / memsz153.zip / DEBUG.C < prev    next >
C/C++ Source or Header  |  1992-12-02  |  5KB  |  161 lines

  1. /******************************************************************** DEBUG.C
  2.  *                                        *
  3.  *  Debugging Aids                                *
  4.  *                                        *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_WIN
  8. #include <os2.h>
  9.  
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #include "debug.h"
  15.  
  16.  
  17. /****************************************************************************
  18.  *                                        *
  19.  *             Display Debug Message                    *
  20.  *                                        *
  21.  ****************************************************************************/
  22.  
  23. extern VOID Debug ( HWND hwnd, char *Message, ... )
  24. {
  25.  /***************************************************************************
  26.   * Local Declarations                                *
  27.   ***************************************************************************/
  28.  
  29.   va_list Marker ;
  30.   char Text [500] ;
  31.  
  32.  /***************************************************************************
  33.   * Format the debug message.                            *
  34.   ***************************************************************************/
  35.  
  36.   va_start ( Marker, Message ) ;
  37.   vsprintf ( Text, Message, Marker ) ;
  38.   va_end ( Marker ) ;
  39.  
  40.  /***************************************************************************
  41.   * Display the log message and wait for the user to press ENTER.        *
  42.   ***************************************************************************/
  43.  
  44.   WinMessageBox ( HWND_DESKTOP, hwnd, Text, "Debug", 0, MB_ENTER ) ;
  45. }
  46.  
  47. /****************************************************************************
  48.  *                                        *
  49.  *               Log Debug Message                    *
  50.  *                                        *
  51.  ****************************************************************************/
  52.  
  53. extern VOID Log ( char *Message, ... )
  54. {
  55.  /***************************************************************************
  56.   * Local Declarations                                *
  57.   ***************************************************************************/
  58.  
  59.   FILE *Log ;
  60.   va_list Marker ;
  61.  
  62.  /***************************************************************************
  63.   * Try to open the log file.  If unsuccessful, just return.            *
  64.   ***************************************************************************/
  65.  
  66.   Log = fopen ( "LOG", "at" ) ;
  67.  
  68.   if ( Log == NULL )
  69.     return ;
  70.  
  71.  /***************************************************************************
  72.   * Format and write the message to the log file.                *
  73.   ***************************************************************************/
  74.  
  75.   va_start ( Marker, Message ) ;
  76.   vfprintf ( Log, Message, Marker ) ;
  77.   va_end ( Marker ) ;
  78.  
  79.  /***************************************************************************
  80.   * Close the log file and return.                        *
  81.   ***************************************************************************/
  82.  
  83.   fclose ( Log ) ;
  84. }
  85.  
  86. /****************************************************************************
  87.  *                                        *
  88.  *                Open Timer for Use                    *
  89.  *                                        *
  90.  ****************************************************************************/
  91.  
  92. extern HFILE OpenTimer ( VOID )
  93. {
  94.   HFILE Handle ;
  95.   USHORT Action ;
  96.  
  97.   if ( DosOpen ( "TIMER$", &Handle, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) )
  98.   {
  99.     return ( -1 ) ;
  100.   }
  101.  
  102.   return ( Handle ) ;
  103. }
  104.  
  105. /****************************************************************************
  106.  *                                        *
  107.  *                Close Timer                    *
  108.  *                                        *
  109.  ****************************************************************************/
  110.  
  111. extern VOID CloseTimer ( HFILE Handle )
  112. {
  113.   DosClose ( Handle ) ;
  114. }
  115.  
  116. /****************************************************************************
  117.  *                                        *
  118.  *             Read Time from HRTIMER.SYS                *
  119.  *                                        *
  120.  ****************************************************************************/
  121.  
  122. extern BOOL GetTime ( HFILE Handle, PTIMESTAMP pts )
  123. {
  124.   USHORT ByteCount ;
  125.  
  126.   if ( DosRead ( Handle, pts, sizeof(*pts), &ByteCount ) )
  127.     return ( FALSE ) ;
  128.  
  129.   return ( TRUE ) ;
  130. }
  131.  
  132. /****************************************************************************
  133.  *                                        *
  134.  *               Calculate Elapsed Time                *
  135.  *                                        *
  136.  ****************************************************************************/
  137.  
  138. extern ULONG ElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs )
  139. {
  140.   ULONG ulMsecs, ulNsecs;
  141.   TIMESTAMP tsStart, tsStop ;
  142.  
  143.   tsStart = *ptsStart ;               // De-reference timestamp
  144.                           //     structures for speed
  145.   tsStop  = *ptsStop ;
  146.  
  147.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  148.  
  149.   if( tsStart.ulNs > tsStop.ulNs )          // If nanosecond overflow ...
  150.   {
  151.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  152.     ulMsecs--;                          // Adjust milliseconds
  153.   }
  154.   else
  155.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  156.  
  157.   *pulNs = ulNsecs ;
  158.  
  159.   return ( ulMsecs ) ;
  160. }
  161.